package com.self.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class LockMain {

    static ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    public static void main(String[] args) {


//        testSelfLatch();

        testSelfSemaphore();
    }

    private static void testSelfLatch() {

        SelfCountDownLatch selfCountDownLatch = new SelfCountDownLatch(2);

        new Thread(() -> {

            try {
                LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2));
                System.out.println(Thread.currentThread().getName()+",执行结束");
            } catch (Exception e) {

            } finally {
                selfCountDownLatch.countDown();
            }

        },"线程1").start();

        new Thread(() -> {

            try {
                LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(10));
                System.out.println(Thread.currentThread().getName()+",执行结束");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                selfCountDownLatch.countDown();
            }

        },"线程2").start();

        try {
            selfCountDownLatch.await();
            System.out.println("两个线程已完成任务");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void testSelfSemaphore() {

        SelfSemaphore selfSemaphore = new SelfSemaphore(2);

        new Thread(() -> {

            try {
                selfSemaphore.acquire();
                System.out.println(Thread.currentThread().getName()+",获取到许可");

                LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(10));
                System.out.println(Thread.currentThread().getName()+",执行结束");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                selfSemaphore.release();
            }

        },"线程1").start();

        new Thread(() -> {

            try {
                selfSemaphore.acquire();
                System.out.println(Thread.currentThread().getName()+",获取到许可");
                LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(15));
                System.out.println(Thread.currentThread().getName()+",执行结束");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                selfSemaphore.release();
            }

        },"线程2").start();

        LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(3));

        try {
            selfSemaphore.acquire();
            System.out.println("主线程获取到许可");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            selfSemaphore.release();
        }
    }

    private static void testReadLock() {

        readWriteLock.readLock().lock();
        try {

            System.out.println(Thread.currentThread().getName()+",获取到读锁");
            LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(10));

        } catch (Exception e) {

        } finally {
            readWriteLock.readLock().unlock();
        }
    }
}
